/**
 * 判断对象是否有重复值
 */
import _ from 'lodash'
export function checkJson(bodyheadlist, bodydatalist) {
  if (bodydatalist.length === 1 || bodydatalist.length === 0) {
    return { flag: true }
  }
  const str = []
  const isOnlyArr = bodyheadlist.filter(item => item.bytisonly === 0 && item.intpreset === 0)
  const keyNameObj = {}
  isOnlyArr.forEach(item => {
    keyNameObj[item.fieldname] = item.fieldshowname
  })
  const keyCountObj = {}
  const conditionArr = ['', null, 'null', undefined]
  Object.keys(keyNameObj).forEach(prop => {
    const arr = []
    for (let i = 0; i < bodydatalist.length; i++) {
      if (conditionArr.includes(bodydatalist[i][prop])) {
        continue
      }
      arr.push(bodydatalist[i][prop])
    }
    if (_.uniq(arr).length !== arr.length) {
      keyCountObj[prop] = 1
      str.push('【' + keyNameObj[prop] + '】')
    } else {
      keyCountObj[prop] = 0
    }
  })
  if (Object.keys(keyCountObj).every(item => !keyCountObj[item])) {
    return { flag: true }
  } else {
    return { flag: false, msg: str.join('') + '存在重复值,请检查' }
  }
}

// 判断json某个字段是否有重复值
//  true 有重复值
//  false  无重复值
export function checkUniq(arr, prop) {
  if (arr.length === 0 || arr.length === 1) {
    return false
  }
  if (_.uniqBy(arr, prop).length !== arr.length) {
    return true
  }
  return false
}

// 判断两个对象不同值，输出key和value
export function getDifferent(json1, json2) {
  for (var key in json1) { // 循环遍历其中一个json对象
    if (typeof (json1[key]) !== 'object') {
      if (json1[key] === json2[key]) {
        delete json1[key]
        delete json2[key]
      }
    } else {
      if ((!json1[key] && !json2[key])) {
        delete json1[key]
        delete json2[key]
      } else if (Array.isArray[json1[key]] && json1[key].length > 0) {
        for (let i = 0; i < json1[key].length; i++) {
          getDifferent(json1[key][i], json2[key][i])
        }
      } else {
        getDifferent(json1[key], json2[key])
      }
    }
  }
  return json2
}

// 去除getDifferent 返回结果中的空数组与空对象
export function filterTruthy(json) {
  for (var key in json) {
    if (Array.isArray(json[key])) {
      if (json[key].length) {
        json[key] = json[key].filter(item => Object.keys(item).length)
        !json[key].length && delete json[key]
      } else {
        delete json[key]
      }
    } else if (typeof json[key] === 'object') {
      if (Object.keys(json[key]).length) {
        filterTruthy(json[key])
      } else {
        delete json[key]
      }
    }
  }
  return json
}

// 过滤单据的 json 使用方法
// const json = getDifferent(this._.cloneDeep(jsonOld), this._.cloneDeep(jsonNew))
// const json1 = filterTruthy(json)
// const json2 = filterTruthy(json1)

// 获取拼音首字母+全拼的字符串 依赖pinyinjs 已经在public index.html中引入
export function allPinYin(str) {
  // eslint-disable-next-line
  return pinyinUtil.getFirstLetter(str).toLowerCase() + pinyinUtil.getPinyin(str).replace(/\s/g, '')
}

// 获取拼音与汉字之间的间隔
export function getDots(num = 10) {
  return ' '.repeat(num)
}

// 判断一个日期是不是在两个日期之间 >= and <
export function isInDateRange(range, cur) {
  const curMs = +new Date(cur)
  const startMs = +new Date(range[0])
  const endMs = +new Date(range[1])
  return curMs >= startMs && curMs < endMs
}
